package com.magic.gre.ui.dialog;

import android.annotation.SuppressLint;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.magic.gre.GREApp;
import com.magic.gre.R;
import com.magic.gre.base.dialog.BaseMVPDialog;
import com.magic.gre.entity.AnswerBean;
import com.magic.gre.mvp.contract.AnswerDialogContract;
import com.magic.gre.mvp.presenter.AnswerDialogPresenterImpl;
import com.magic.gre.recording.AudioPlayManager;
import com.magic.gre.recording.IAudioPlayListener;
import com.noname.lib_base_java.net.download.DownloadInfo;
import com.noname.lib_base_java.util.ToastUtil;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;

/**
 * Author: Lmoumou
 * Date: 2019-01-09 14:28
 * 文件名: AnswerDialog
 * 描述:
 */
public class AnswerDialog extends BaseMVPDialog<AnswerDialogPresenterImpl> implements AnswerDialogContract.View {
    private AnswerBean answerBean;
    private OnAnserDialogLiatener onAnserDialogLiatener;
    private TextView continueTv;
    private String content;
    private TextView pronunciationTv;
    private RxPermissions rxPermissions;
    private Handler handler = new Handler();
    private TextView newWordsTv;

    public void setOnAnserDialogLiatener(OnAnserDialogLiatener onAnserDialogLiatener) {
        this.onAnserDialogLiatener = onAnserDialogLiatener;
    }

    @Override
    protected int setViewId() {
        return R.layout.dialog_answer;
    }

    @Override
    protected void initBundle(Bundle bundle) {

    }

    @Override
    protected void initView(View view) {
        newWordsTv = view.findViewById(R.id.new_words_tv);

        if (this.answerBean != null) {
            newWordsTv.setSelected(answerBean.getUnriped() == 1);
            continueTv = view.findViewById(R.id.continue_tv);
            TextView wordsTv = view.findViewById(R.id.words_tv);
            wordsTv.setText(answerBean.getName());
            pronunciationTv = view.findViewById(R.id.pronunciation_tv);
            pronunciationTv.setText(answerBean.getSymbol());
            TextView explainTv = view.findViewById(R.id.explain_tv);
            explainTv.setText(answerBean.getMeaning());
            continueTv.setText(content);
            newWordsTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPresenter.pNewWordsSave(answerBean.getId());
                }
            });
        }

        view.findViewById(R.id.details_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAnserDialogLiatener != null) {
                    onAnserDialogLiatener.onDetails(answerBean);
                }
            }
        });

        view.findViewById(R.id.continue_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (onAnserDialogLiatener != null) {
                    dismissThis(isResumed());
                    onAnserDialogLiatener.onNext();
                }
            }
        });

        if (pronunciationTv != null) {
            pronunciationTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Drawable[] compoundDrawables = pronunciationTv.getCompoundDrawables();
                    AnimationDrawable animation = (AnimationDrawable) compoundDrawables[2];
                    String dirPath = Environment.getExternalStorageDirectory() + File.separator + GREApp.getContext().getPackageName() + File.separator + "Download" + File.separator + answerBean.getThesaurusId();
                    playVoice(animation, answerBean.getVoicePath(dirPath));
                }
            });
        }
    }


    public void showThis(FragmentManager manager, String tag, AnswerBean answerBean, String s) {
        super.showThis(manager, tag);
        this.answerBean = answerBean;
        this.content = s;
    }


    @Override
    public void dismissThis(boolean isResume) {
        super.dismissThis(isResume);
    }

    @Override
    public void onResume() {
        super.onResume();
        Window win = getDialog().getWindow();
        WindowManager.LayoutParams params = win.getAttributes();
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        win.setAttributes(params);
    }

    @Override
    protected float halfTpValue() {
        return 0.0F;
    }

    @Override
    protected boolean setOutSide() {
        return false;
    }

    @Override
    protected boolean clickBack() {
        return false;
    }


    /**
     * 延迟0.5秒播放音频
     *
     * @param animation
     * @param voicePath
     */
    @SuppressLint("CheckResult")
    private void playVoice(final AnimationDrawable animation, final String voicePath) {
        File file = new File(voicePath);
        if (!file.exists()) {
//            ToastUtil.getInstance().showNormal(mContext, "资源不存在");
            return;
        }
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (voicePath == null) {
                    return;
                }
                AudioPlayManager.getInstance().stopPlay();
                Uri audioUri = Uri.parse(voicePath);
                AudioPlayManager.getInstance().startPlay(getContext(), audioUri, new IAudioPlayListener() {
                    @Override
                    public void onStart(Uri var1) {
                        if (animation != null)
                            animation.start();

                    }

                    @Override
                    public void onStop(Uri var1) {
                        if (animation != null) {
                            animation.stop();
                            animation.selectDrawable(0);

                        }

                    }

                    @Override
                    public void onComplete(Uri var1) {
                        if (animation != null) {
                            animation.stop();
                            animation.selectDrawable(0);
                        }
                    }
                });
            }
        }, 500);
    }

    @Override
    protected AnswerDialogPresenterImpl creatPresenter() {
        return new AnswerDialogPresenterImpl(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
        mPresenter.detachView();
    }

    @Override
    public void vNewWordsSave() {
        if (newWordsTv.isSelected()) {
            ToastUtil.getInstance().showNormal(mContext, "已移出生词本");
        } else {
            ToastUtil.getInstance().showNormal(mContext, "已加入生词本");
        }
        newWordsTv.setSelected(!newWordsTv.isSelected());
    }

    @Override
    public void vDownload(DownloadInfo info) {

    }

    @Override
    public void vDownloadFail(String msg) {

    }

    public interface OnAnserDialogLiatener {
        void onNext();

        void onDetails(AnswerBean answerBean);
    }
}
